//
//  CQTabBarController.m
//  01-OC微博
//
//  Created by 陈乾 on 15/10/21.
//  Copyright © 2015年 陈乾. All rights reserved.
//

#import "CQTabBarController.h"
#import "UIImage+CQImage.h"
#import "CQTabBar.h"
#import "CQHomeTableViewController.h"
#import "CQNewsTableViewController.h"
#import "CQDiscoverTableViewController.h"
#import "CQProfileTableViewController.h"
#import "CQNavgationController.h"

@interface CQTabBarController ()<CQTabBarDelegate>
@property (nonatomic, strong) NSMutableArray *itemArr;

@end

@implementation CQTabBarController

#pragma mark - 懒加载
- (NSMutableArray *)itemArr
{
    if (!_itemArr) {
        _itemArr = [NSMutableArray array];
    }
    return _itemArr;
}
#pragma mark -viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //添加所以的子控制器
    [self addAllChildViewController];
    
    [self setUptabBar];
    
    
}
#pragma mark - 添加自己的tabBar
- (void)setUptabBar
{
    
    //    把系统的tabBar 换成我自定义的
    CQTabBar *tabBar = [[CQTabBar alloc] initWithFrame:self.tabBar.bounds];
    
    //    白色的背景
    tabBar.backgroundColor = [UIColor whiteColor];
    
    //    设置代理
    tabBar.delegate = self;
    
    //把装了系统tabbarItem的数组传递
    tabBar.itemArr = self.itemArr;
    
    //    添加view上
    [self.tabBar addSubview:tabBar];

}

//当我们把自己的tabBar添加到系统的tabBar上 需要在系统的tabBar加载的时候把他上面的按钮移除
-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarButton removeFromSuperview];
        }
    }
    
}

#pragma mark - 代理方法
-(void)tabBar:(CQTabBar *)tabBar andTag:(NSInteger)tag
{
    self.selectedIndex = tag;

}


#pragma mark - 一些初始化方法

/**
 *  当类或者子类第一次使用的时候调用
 用来初始化类
 */
+(void)initialize
{
    
    //这样比appearance 更安全
    UITabBarItem *item  =  [UITabBarItem appearanceWhenContainedIn:self, nil];
    //选中状态的颜色为 橘色
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    
}

/**
 *  当类第一次加载到内存的时候调用
 */
//+(void)load
//{
//
//}


#pragma mark - 添加所有的子控制器
- (void)addAllChildViewController
{
    
    CQHomeTableViewController *home  = [[CQHomeTableViewController alloc]init];
    [self addJustOneChildViewController:home Image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[UIImage imageOriginalWithImageName:@"tabbar_home_selected"] title:@"首页"];
    
    
    CQNewsTableViewController *message  = [[CQNewsTableViewController alloc]init];
    [self addJustOneChildViewController:message Image:[UIImage imageNamed:@"tabbar_message_center"] selectedImage:[UIImage imageOriginalWithImageName:@"tabbar_message_center_selected"] title:@"消息"];
    
    
    CQDiscoverTableViewController *discover  = [[CQDiscoverTableViewController alloc]init];
    [self addJustOneChildViewController:discover Image:[UIImage imageNamed:@"tabbar_discover"] selectedImage:[UIImage imageOriginalWithImageName:@"tabbar_discover_selected"] title:@"发现"];
    
    
    CQProfileTableViewController *profile  = [[CQProfileTableViewController alloc]init];
    [self addJustOneChildViewController:profile Image:[UIImage imageNamed:@"tabbar_profile"] selectedImage:[UIImage imageOriginalWithImageName:@"tabbar_profile_selected"] title:@"我TT"];
    
    
}
/**
 *  添加单独一个控制器
 */
- (void)addJustOneChildViewController:(UIViewController *)vc Image:(UIImage*)image selectedImage:(UIImage *)selectedImage title:(NSString*)title
{
    //  设置属性
    vc.title = title;
    //  vc.tabBarItem.title = title;
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectedImage;
    //超过2位数就 只显示一个小小的图标
    vc.tabBarItem.badgeValue = @"10";
    //   把系统的tabBarItem存起来
    [self.itemArr addObject:vc.tabBarItem];
    //   把vc变成导航控制器
    CQNavgationController *nav = [[CQNavgationController alloc]initWithRootViewController:vc];
    //    添加到tabBarController
    [self addChildViewController:nav];
}

@end
